import React from 'react'
import { Route, Routes, useLocation, Navigate } from "react-router-dom"
import Login from "../pages/login/login"
import Reguser from "../pages/login/reguser"
import { getCookie } from '../untils/auth'
import MainNav from "../pages/menu/mainNav"
import SystemUser from "../pages/systemUser"
import SystemTeam from "../pages/systemTeam"
import SystemRegister from "../pages/systemRegister"
import SystemAnnouncement from "../pages/systemAnnouncement"
import SystemHospitalAnnouncement from "../pages/systemHospitalAnnouncement"
import SystemRotograph from "../pages/systemRotograph"
import MedicalTreatment from "../pages/medicalTreatment"


export default function index() {
    const location = useLocation()
    const { pathname } = location
    const router = [

        {
            path: "/login",
            element: <Login />,
            auth: false
        },
        {
            path: "/reguser",
            element: <Reguser />,
            auth: false
        },
        {
            path: "/mainNav",
            element: <MainNav />,
            auth: true,
            children: [
                {
                    path: "systemUser",
                    element: <SystemUser />,
                    auth: true,
                },
                {
                    path: 'systemTeam',
                    element: <SystemTeam />,
                    auth: true,
                },
                {
                    path: 'systemRegister',
                    element: <SystemRegister />,
                    auth: true,
                },
                {
                    path: 'systemAnnouncement',
                    element: <SystemAnnouncement />,
                    auth: true,
                },
                {
                    path: 'systemHospitalAnnouncement',
                    element: <SystemHospitalAnnouncement />,
                    auth: true,
                },
                {
                    path: 'systemRotograph',
                    element: <SystemRotograph />,
                    auth: true,
                },
                {
                    path: 'medicalTreatment',
                    element: <MedicalTreatment />,
                    auth: true,
                },
            ]
        },
        {
            path: "/",
            element: <Navigate to="/mainNav" />,
        },
    ]
    const RouteNav = (router) => {
        return (
            router.map((item) => {
                return (
                    <Route path={item.path} element={item.auth && !getCookie("token") ?
                        <Navigate to='/login' replace={true}></Navigate> :
                        getCookie("token") && pathname === "/login" ?
                            <Navigate to="/mainNav"></Navigate> : item.element} key={item.path}>
                        {
                            item.children && RouteNav(item.children)
                        }
                    </Route>
                )
            })
        )
    }
    return (
        <Routes>
            {RouteNav(router)}
        </Routes>
    )
}
